package com.restaurant.springboot.controller;

import com.restaurant.springboot.entity.DishesEntity;
import com.restaurant.springboot.service.DishesService;
import com.restaurant.springboot.utils.RespCode;
import com.restaurant.springboot.utils.RespEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/admin/dishes")
public class AdminDishesController {

    @Autowired
    private DishesService dishesService;

    /**
     * 修改菜品信息
     * @param dishes
     * @return
     */
    @PutMapping("")
    public RespEntity updateDishes(@RequestBody DishesEntity dishes){
        if(dishesService.saveDishes(dishes)){
            return new RespEntity(RespCode.SUCCESS);
        }else{
            return new RespEntity(RespCode.ERROR, "修改菜品时发生错误", null);
        }
    }


    /**
     * 新增菜品
     * @param dishes
     * @return
     */
    @PostMapping("")
    public RespEntity addDishes(@RequestBody DishesEntity dishes){
        if(dishesService.saveDishes(dishes)){
            return new RespEntity(RespCode.SUCCESS);
        }else{
            return new RespEntity(RespCode.ERROR, "新增菜品时发生错误", null);
        }
    }

    /**
     * 删除菜品
     */
    @DeleteMapping("{dishesId}")
    public RespEntity deleteDishes(@PathVariable int dishesId) {
        try {
            dishesService.deleteDishesByDishesId(dishesId);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespEntity(RespCode.ERROR, "删除菜品时发生错误", null);
        }
        return new RespEntity(RespCode.SUCCESS);
    }

}
